package com.sky.controller.admin;

import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminSetmealContraller")
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealContraller {
    @Autowired
    private SetmealService setmealService;
    /*
    分页查询
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分页查询"+setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /*
    根据id查询套餐
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealDTO> getById(@PathVariable Long id){
        log.info("根据id查询套餐+id"+id);
        SetmealDTO setmealDTO = setmealService.getById(id);
        return Result.success(setmealDTO);
    }

    /*
    添加套餐
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(value = "setmealCache",key="#setmealDTO.categoryId")//先删除某粉套餐缓存
    public Result save(@RequestBody SetmealDTO setmealDTO){//RequestBody: 将json格式的数据转换为java对象
        log.info("新增套餐");
        setmealService.save(setmealDTO);
        return Result.success();
    }

    /*
    套餐删除
     */
    @DeleteMapping
    @ApiOperation("套餐删除")
    @CacheEvict(value = "setmealCache",allEntries = true)//删除所有套餐缓存
    public Result delete(@RequestParam List<Long> ids){//RequestParam：是将请求参数的值绑定到方法参数上
        log.info("套餐删除");
        setmealService.delete(ids);
        return Result.success();
    }

    /*
    菜品起售与停
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售与停")
    @CacheEvict(value = "setmealCache",allEntries = true)//删除所有套餐缓存
    public Result startOrStop(@PathVariable Integer status, Long id){//PathVariable: 将路径参数的值绑定到方法参数上
        log.info("菜品起售与停");
        setmealService.startOrStop(status,id);
        return Result.success();
    }

    /*
    修改套餐
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(value = "setmealCache",allEntries = true)//删除所有套餐缓存
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐");
        setmealService.update(setmealDTO);
        return Result.success();
    }

}
